#ifndef CSerialPort_HPP
#define CSerialPort_HPP

#include <stdio.h>
#include <pthread.h>

// Inspired by:
// https://github.com/todbot/arduino-serial
class CSerialPort
{
public:
   /**
    * @brief Constructor
    * @param device Name of device of serial port. E.g. /dev/ttyUSB0 for Arduino Duemilanove
    */ 
   CSerialPort(const char* device);
   
   /**
    * @brief Destructor
    */
   ~CSerialPort();
   
   /**
    * @brief Sends string to COM port
    * @param str String to send
    */
   size_t send(const unsigned char* str, size_t len);

   /**
    * @brief Receives data from Serial port
    * @param buffer Allocated buffer to put read data to.
    */
   size_t recv(unsigned char* buffer, size_t length);

   /**
    * @brief Checks, whether serial port is opened
    *        Criteria: FILE* pointer is NULL or not
    * @return True if port is opened
    */
   bool isOpened();
protected:

private:
   /**
    * @brief Default constructor
    * !!! Unimplemented !!!
    */
   CSerialPort();

   /**
    * @brief Sends only one byte to port
    * @param b Byte to be sent
    * Deprecated
    */
   // FIXME: remove this method
   bool sendByte(unsigned char b);

   const char* mpDeviceName;
   const int mBaudRate;
   int mSerialHndl;
   pthread_mutex_t mLock;     // Serial port should be synchronized.
};

#endif // CSerialPort_HPP